package com.pirestupppai.ui.activity;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.bean.DetailData;
import com.pirestupppai.bean.FeedItem;
import com.pirestupppai.common.Constant;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.utils.Utils;
import com.pirestupppai.widgets.helper.ScaleAlphaPageTransformer;
import com.pirestupppai.widgets.view.FeedView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 详情页
 *
 * @author: xiaoluo
 * @date: 2017-04-24 15:49
 */
public class DetailActivity extends BaseActivity {
    private final static String TAG = DetailActivity.class.getSimpleName();

    @BindView(R.id.detail_viewpager)
    ViewPager viewPager;
    @BindView(R.id.detail_viewpager_parent)
    LinearLayout viewPagerParent;
    @BindView(R.id.detail_bottombar_tv)
    TextView workCountTv;

    private int mId;
    private int mPage = 1;
    private int mSize = 10;
    private int mIndex = 0;
    private boolean hasMore = false;
    private List<FeedItem> mFeedItemList = new ArrayList<>();
    private List<View> mFeedViewList = new ArrayList<>();
    private ScaleAlphaPageTransformer mTransformer = new ScaleAlphaPageTransformer();
    private ViewPagerAdapter mAdapter;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initViews() {
        mId = getIntent().getIntExtra(Constant.IntentKey.ID, -1);
        LinearLayout.LayoutParams viewLp = (LinearLayout.LayoutParams) viewPager.getLayoutParams();
        viewLp.rightMargin = Utils.dpToPx(mContext, 30);
        viewLp.leftMargin = Utils.dpToPx(mContext, 30);
        viewLp.topMargin = Utils.dpToPx(mContext, 10);
        viewLp.bottomMargin = Utils.dpToPx(mContext, 0);
        viewPager.setLayoutParams(viewLp);

        mAdapter = new ViewPagerAdapter(mFeedViewList);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(2);
        mTransformer.setType(true, true);
        viewPager.setPageTransformer(true, mTransformer);

    }

    private void setDataList(List<FeedItem> list) {
        for (int i = 0 ; i < list.size(); i ++) {
            FeedView view = new FeedView(mContext);
            view.setViews(list.get(i), FeedView.ShowType.SHOW_TYPE_DETAIL);
            mFeedViewList.add(view);
        }
        mAdapter.setList(mFeedViewList);
    }

    @Override
    protected void initListeners() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                viewPagerParent.invalidate();
            }

            @Override
            public void onPageSelected(int position) {
                if (hasMore && position == mFeedViewList.size() - 1) {
                    getMoreList();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPagerParent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return viewPager.dispatchTouchEvent(event);
            }
        });
    }

    private void getMoreList() {
        mPage ++;
        NetworkRequest.getInstance().getFeedDetailList(mId, mPage, mSize, this,
                new TupaiSubscriber<DetailData>(mContext) {
                    @Override
                    protected void requestSuccess(DetailData data) {
                        if (data.getPosts().size() != 0) {
                            mFeedItemList.addAll(data.getPosts());
                            setDataList(data.getPosts());
                        }

                        hasMore = data.getPosts().size() >= mSize;
                    }

                    @Override
                    protected void requestError(String error) {

                    }
                });
    }

    @Override
    protected void initLogic() {
        NetworkRequest.getInstance().getFeedDetailList(mId, mPage, mSize, this,
                new TupaiSubscriber<DetailData>(mContext) {
                    @Override
                    protected void requestSuccess(DetailData data) {
                        if (data == null || data.getPosts() == null) {
                            hasMore = false;
                            return;
                        }
                        mIndex = data.getIndex();
                        if (data.getPosts().size() != 0) {
                            mFeedViewList.clear();
                            mFeedItemList.clear();
                            mFeedItemList.addAll(data.getPosts());
                            setDataList(mFeedItemList);
                            viewPager.setCurrentItem(mIndex);
                            workCountTv.setText("已有" + mFeedItemList.get(0).getReply_count() + "个作品");
                        }

                        hasMore = data.getPosts().size() >= mSize;
                    }

                    @Override
                    protected void requestError(String error) {

                    }
                });
    }

    @Override
    public void onClick(View v) {

    }



    private class ViewPagerAdapter extends PagerAdapter {
        private List<View> mList;

        public ViewPagerAdapter() {
            super();
        }

        ViewPagerAdapter(List<View> list) {
            super();
            this.mList = list;
        }

        public void setList(List<View> list) {
            this.mList = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(Object object) {
            int length = mList.size();
            for (int i = 0; i < length; i++) {
                if (object.equals(mList.get(i))) {
                    return i;
                }
            }
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView(mList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(mList.get(position));
            return mList.get(position);
        }
    }

}
